function ListNode(val){
    this.val = val
    this.next = null
}
var middleNode = function(head) {
    let p = head
    let n = 0
    while (p != null) {
        p = p.next
        n++
    }
    let middle = Math.floor(n / 2)
    let q = head
    let num = 0
    while (q != null) {
        if (n % 2 == 0) {
            q = q.next
            num++
            while (num == middle + 1) return q
        }
        if (n % 2 != 0) {
            q = q.next
            num++
            while (num == middle) return q
        }
    }
};
let head = new ListNode(1)
head.next = new ListNode(2)
head.next.next = new ListNode(3)
head.next.next.next = new ListNode(4)
head.next.next.next.next = new ListNode(5)

console.log(middleNode(head))